package com.rytx.web.controller.ysp;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rytx.common.core.domain.model.ArtUser;
import com.rytx.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.enums.BusinessType;
import com.rytx.system.domain.ShippingAddresses;
import com.rytx.system.service.IShippingAddressesService;
import com.rytx.common.utils.poi.ExcelUtil;
import com.rytx.common.core.page.TableDataInfo;

/**
 * 收货地址Controller
 *
 * @author rytx
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/system/addresses")
public class AddressesController extends BaseController
{
    @Autowired
    private IShippingAddressesService shippingAddressesService;

    /**
     * 查询收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:addresses:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShippingAddresses shippingAddresses)
    {
        startPage();
        List<ShippingAddresses> list = shippingAddressesService.selectShippingAddressesList(shippingAddresses);
        return getDataTable(list);
    }

    @GetMapping("/getList")
    public TableDataInfo getList()
    {
        startPage();
        ShippingAddresses shippingAddresses = null;
        try {
            shippingAddresses = new ShippingAddresses();
            shippingAddresses.setUserId(getLoginUser().getArtUser().getId());
        } catch (Exception e) {
             logger.error("获取用户信息失败", e);
        }
        List<ShippingAddresses> list = shippingAddressesService.selectShippingAddressesList(shippingAddresses);
        return getDataTable(list);
    }
    /**
     * 导出收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:addresses:export')")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShippingAddresses shippingAddresses)
    {
        List<ShippingAddresses> list = shippingAddressesService.selectShippingAddressesList(shippingAddresses);
        ExcelUtil<ShippingAddresses> util = new ExcelUtil<ShippingAddresses>(ShippingAddresses.class);
        util.exportExcel(response, list, "收货地址数据");
    }

    /**
     * 获取收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:addresses:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shippingAddressesService.selectShippingAddressesById(id));
    }

    @GetMapping("/info/{id}")
    public AjaxResult getAddInfo(@PathVariable("id") Long id)
    {
        ShippingAddresses shippingAddresses = new ShippingAddresses();
        shippingAddresses.setId(id);
        try {
            LoginUser loginUser = getLoginUser();
            shippingAddresses.setUserId(loginUser.getArtUser().getId());
        } catch (Exception e) {
            logger.error("获取用户信息失败", e);
            return error(e.getMessage());
       }
        return success(shippingAddressesService.selectShippingAddressesByIdAndUserId(shippingAddresses));
    }

    /**
     * 新增收货地址
     */
  //  @PreAuthorize("@ss.hasPermi('system:addresses:add')")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShippingAddresses shippingAddresses)
    {
        try {
            LoginUser loginUser = getLoginUser();
            shippingAddresses.setUserId(loginUser.getArtUser().getId());
        } catch (Exception e) {
            logger.error("获取用户信息失败", e);
            return error(e.getMessage());
        }
        return toAjax(shippingAddressesService.insertShippingAddresses(shippingAddresses));
    }

    /**
     * 修改收货地址
     */
   // @PreAuthorize("@ss.hasPermi('system:addresses:edit')")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShippingAddresses shippingAddresses)
    {
        try {
            LoginUser loginUser = getLoginUser();
            shippingAddresses.setUserId(loginUser.getArtUser().getId());
        } catch (Exception e) {
            logger.error("获取用户信息失败", e);
            return error(e.getMessage());
        }
        return toAjax(shippingAddressesService.updateShippingAddresses(shippingAddresses));
    }

    /**
     * 删除收货地址
     */
//    @PreAuthorize("@ss.hasPermi('system:addresses:remove')")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shippingAddressesService.deleteShippingAddressesByIds(ids));
    }
    /**
     * 修改收货地址默认状态
     */
    @PostMapping("/updatedefaultaddress")
    public AjaxResult updatedefaultaddress(@RequestBody ShippingAddresses shippingAddresses){
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getArtUser().getId();
//        shippingAddressesService.cleardefaultaddress(userid);
        shippingAddressesService.updatedefaultaddress(userid,shippingAddresses.getId());
        return AjaxResult.success();
    }
    /**
     * 获取用户默认收货地址
     */
    @GetMapping("/getdefaultaddress")
    public AjaxResult getdefaultaddress(){
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getArtUser().getId();

        return success(shippingAddressesService.getdefaultaddress(userid));
    }
}
